package com.zhao.modleclass.activity;

import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.zhao.modleclass.R;

/**
 * Created by 张博文 on 2016/12/16.
 */

public class SettingFeedback extends AppCompatActivity {
    private EditText mEditText;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_feedback);
        //设置状态栏的颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //获取样式中的属性值
            TypedValue typedValue = new TypedValue();
            this.getTheme().resolveAttribute(Color.parseColor("#FF4081"), typedValue, true);
            int[] attribute = new int[]{Color.parseColor("#FF4081")};
            TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);
            int color = array.getColor(0, Color.parseColor("#FF4081"));
            array.recycle();
            window.setStatusBarColor(color);
        }
        mEditText= (EditText) findViewById(R.id.iv_setting_etsay);
        mEditText.addTextChangedListener(mTextWaycher);
    }

    TextWatcher mTextWaycher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            editStart = mEditText.getSelectionStart();
            editEnd = mEditText.getSelectionEnd();
            if (temp.length() < 5){
                Toast.makeText(SettingFeedback.this,"最少5个字，尽量吐槽吧！亲，你太热情啦！一次最多150字哦~",Toast.LENGTH_SHORT).show();
                s.delete(editStart-1,editEnd);
                int tempSelection = editStart;
                mEditText.setText(s);
                mEditText.setSelection(tempSelection);
            }else if (temp.length() > 150){
                Toast.makeText(SettingFeedback.this,"亲，你太热情啦！一次最多150字哦~",Toast.LENGTH_SHORT).show();
                s.delete(editStart-1,editEnd);
                int tempSelection = editStart;
                mEditText.setText(s);
                mEditText.setSelection(tempSelection);
            }
        }
    };
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_setting_back:
                finish();
                break;
            case R.id.iv_setting_feedGo:
                break;
        }

    }
}
